<?php
require_once('common.php');
if (empty($_GET['code'])) {
    die("Empty code!");
}
if (empty($_GET['plat'])) {
    die("Empty plat!");
}
$code = htmlspecialchars($_GET['code']);
$plat = strtolower(htmlspecialchars($_GET['plat']));
setcookie('version', time());
switch ($plat) {
    case  'gitee':
        $redirect_uri = urlencode(REDIRECT_URL . "?plat=" . $plat);
        $url = "https://gitee.com/oauth/token?grant_type=authorization_code&code={$code}&client_id=" . $oauthList['gitee']['id'] . "&redirect_uri={$redirect_uri}&client_secret=" . $oauthList['gitee']['key'] . "";
        $result = curlHelper($url, [], [], "", 'POST');
        if ($result['detail']['http_code'] == 200) {
            $obj = json_decode($result['body'], true);
            $access_token = $obj['access_token'];
            $url = "https://gitee.com/api/v5/user?access_token={$access_token}";
            $result = curlHelper($url);
            if ($result['detail']['http_code'] == 200) {
                $user = json_decode($result['body'], true);
                $user = ['id' => $user['id'], 'avatar_url' => $user['avatar_url'], 'login' => $user['login'], 'name' => $user['name'], 'plat' => '码云'];
                setcookie('userInfo', json_encode($user));
                header("Location: /");
                die;
            }
        }
        break;
    case  'oschina':
        $redirect_uri = urlencode(REDIRECT_URL . "?plat=" . $plat);
        $url = "https://www.oschina.net/action/openapi/token?grant_type=authorization_code&code={$code}&client_id=" . $oauthList['oschina']['id'] . "&redirect_uri={$redirect_uri}&client_secret=" . $oauthList['oschina']['key'] . "";
        $result = curlHelper($url, [], [], "", 'POST');
        if ($result['detail']['http_code'] == 200) {
            $obj = json_decode($result['body'], true);
            $access_token = $obj['access_token'];
            $url = "https://www.oschina.net/action/openapi/user?access_token={$access_token}";
            $result = curlHelper($url);
            if ($result['detail']['http_code'] == 200) {
                $user = json_decode($result['body'], true);
                $user = ['id' => $user['id'], 'avatar_url' => explode('!', $user['avatar'])[0], 'login' => str_replace('https://my.oschina.net/', '', $user['url']), 'name' => $user['name'], 'plat' => '开源中国'];
                setcookie('userInfo', json_encode($user));
                header("Location: /");
                die;
            }
        }
        break;
    case  'qq':
        $redirect_uri = urlencode(REDIRECT_URL . "?plat=" . $plat);
        $url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&code={$code}&client_id=" . $oauthList['qq']['id'] . "&redirect_uri={$redirect_uri}&client_secret=" . $oauthList['qq']['key'] . "";
        $result = curlHelper($url, [], [], "", 'GET');
        if ($result['detail']['http_code'] == 200) {
            if (preg_match('/access_token=(.*?)&/', $result['body'], $match)) {
                $access_token = $match[1];
                $url = "https://graph.qq.com/oauth2.0/me?access_token={$access_token}";
                $result = curlHelper($url);
                if ($result['detail']['http_code'] == 200) {
                    if (preg_match('/openid":"(.*?)"/', $result['body'], $match)) {
                        $openid = $match[1];
                        $url = "https://graph.qq.com/user/get_user_info?access_token={$access_token}&openid={$openid}&oauth_consumer_key=" . $oauthList['qq']['id'];
                        $result = curlHelper($url);
                        $user = json_decode($result['body'], true);
                        if ($user['ret'] == 0) {
                            $user = ['id' => $openid, 'avatar_url' => $user['figureurl_qq_2'] ?? $user['figureurl_qq_1'], 'login' => $openid, 'name' => $user['nickname'], 'plat' => 'QQ'];
                            setcookie('userInfo', json_encode($user));
                            header("Location: /");
                            die;
                        }
                    }
                }
            }
        }
        break;
    case  'github':
        $redirect_uri = urlencode(REDIRECT_URL . "?plat=" . $plat);
        $url = "https://github.com/login/oauth/access_token?code={$code}&client_id=" . $oauthList['github']['id'] . "&redirect_uri={$redirect_uri}&client_secret=" . $oauthList['github']['key'] . "";
        $result = curlHelper($url);
        if (preg_match('/access_token=(.*?)&/', $result['body'], $match)) {
            $access_token = $match[1];
            $url = "https://api.github.com/user?access_token={$access_token}";
            $result = curlHelper($url, [], ["User-Agent: HammCn", 'Accept: application/json']);
            if ($result['detail']['http_code'] == 200) {
                $user = json_decode($result['body'], true);
                $user = ['id' => $user['id'], 'avatar_url' => '/github.png', 'login' => $user['login'], 'name' => $user['name'], 'plat' => 'Github'];
                setcookie('userInfo', json_encode($user));
                header("Location: /");
                die;
            }else{
                echo 'Faild to get userinfo from Github.<a href="redirect.php">Retry it!</a>';
                die;
            }
        }
        break;
    default:
        die('Error plat');
        break;
}
header('Location: redirect.php');
die;
